package com.pos.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pos.R;

/**
 * @description:通用弹出框dialog
 * @author cai
 * @time:2015年1月20日上午10:26:54
 */
public class CommonDialog {
	private Dialog dialog;
	private View view;
	private LayoutInflater inflater;
	private Button button_confirm, button_cancel;
	private TextView text_msg;

	public CommonDialog(Context context) {
		dialog = new Dialog(context, R.style.dialog);
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.common_dialog_layout, null);
		button_confirm = (Button) view.findViewById(R.id.button_confirm);
		button_cancel = (Button) view.findViewById(R.id.button_cancel);
		text_msg = (TextView) view.findViewById(R.id.text_msg);
		dialog.setContentView(view);
//		Window dialogWindow = dialog.getWindow();
//		WindowManager m = ((Activity) context).getWindowManager();
//		Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
//		WindowManager.LayoutParams p = dialogWindow.getAttributes();
//		// p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
//		p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.65
//		dialogWindow.setAttributes(p);

	}

	/**
	 * showDialog
	 * 
	 * @param message
	 *            提示语
	 * @param buttonOneText
	 *            第一个按钮文字
	 * @param listener_confirm
	 *            第一个按钮事件
	 * @param listener_cancel
	 *            第二个按钮事件
	 */
	public void showDialog(String message, String buttonOneText, View.OnClickListener listener_confirm,
                           View.OnClickListener listener_cancel) {
		text_msg.setText(message);
		button_confirm.setText(buttonOneText);
		button_confirm.setOnClickListener(listener_confirm);
		button_cancel.setOnClickListener(listener_cancel);
		dialog.show();
	}

	/**
	 * showDialog
	 *
	 * @param message
	 *            提示语
	 * @param buttonText
	 *            确定按钮文字
	 * @param listener_cancel
	 *            确定按钮事件
	 */
	public void showDialog(String message, String buttonText, View.OnClickListener listener_confirm) {
		text_msg.setText(message);
		button_confirm.setText(buttonText);
		button_confirm.setOnClickListener(listener_confirm);
		button_cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismissDialog();

			}
		});
		dialog.show();
	}

	public void dismissDialog() {
		dialog.dismiss();
	}

	public boolean isShow() {
		if (dialog.isShowing()) {
			return true;
		} else {
			return false;
		}
	}

	public Dialog getDialog() {
		return dialog;
	}

	public void setCancelable(boolean is) {
		dialog.setCancelable(is);
	}

}
